# Definition for a binary tree node.
from typing import Optional
import re

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

    def __str__(self):
        return f"TreeNode(val={self.val}, left={self.left}, right={self.right})"
class Solution:
    def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
        lst = [(len(t[0]), TreeNode(int(t[1]))) for t in re.findall(r'(-*)(\d+)', traversal)]
        n = len(lst)
        i = 0

        def dfs(cur_lvl: int) -> Optional[TreeNode]:
            nonlocal i
            if (i == n or lst[i][0] != cur_lvl): return None
            node = lst[i][1]
            i += 1
            node.left = dfs(cur_lvl + 1)
            node.right = dfs(cur_lvl + 1)
            return node

        return dfs(0)

a=Solution()
print(a.recoverFromPreorder("1-2--3--4-5--6--7"))
